package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.organization.util.VoucherAdjustOrderDetail;
import com.turing.post.organization.util.VoucherAdjustOrderDetailDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author wzd
 * @2013-03-12
 */
@Stateless(mappedName = "organization.impl.VoucherAdjustOrderDetailDaoImpl")
@Remote
@Local
public class VoucherAdjustOrderDetailDaoImpl implements
		VoucherAdjustOrderDetailDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	/**
	 * 根据条件查询凭证调整单详细列表
	 * 
	 * @param voucherAdjustOrderId
	 *            调整单ID
	 * @param page
	 *            页码
	 * @return 凭证调整单详细列表
	 */
	@SuppressWarnings("unchecked")
	public List<VoucherAdjustOrderDetail> getVoucherAdjustOrderDetailList(
			PCPSWebInput webInput, BigInteger voucherAdjustOrderId, Integer page) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo())
				|| voucherAdjustOrderId == null)
			return null;
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "select * from VoucherAdjustOrderDetail where VoucherAdjustOrderId =?1 order by SeqNo";
		Query query = _em
				.createNativeQuery(sql, VoucherAdjustOrderDetail.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		query.setParameter(1, voucherAdjustOrderId);
		return query.getResultList();
	}

	/**
	 * 根据条件查询凭证调整单详细总数
	 * 
	 * @param voucherAdjustOrderId
	 *            调整单ID
	 * @return 凭证调整单详细录数
	 */
	@Override
	public BigInteger getVoucherAdjustOrderDetailListTotal(
			PCPSWebInput webInput, BigInteger voucherAdjustOrderId) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo())
				|| voucherAdjustOrderId == null)
			return null;

		String sql = "select nvl(count(*),0) from VoucherAdjustOrderDetail where VoucherAdjustOrderId =?1";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, voucherAdjustOrderId);
		return new BigInteger(query.getSingleResult().toString());
	}

	/**
	 * 新增凭证调整单详细
	 * 
	 * @param VoucherAdjustOrderDetail
	 *            凭证调整单详细
	 * @throws PCPSApplicationException
	 */
	@Override
	public void insertVoucherAdjustOrderDetail(
			VoucherAdjustOrderDetail voucherAdjustOrderDetail)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, voucherAdjustOrderDetail);
	}

	/**
	 * 根据条件查询凭证调整单详细列表---不分页
	 * 
	 * @param voucherAdjustOrderId
	 *            调整单ID
	 * @return 凭证调整单详细列表
	 */
	@SuppressWarnings("unchecked")
	public List<VoucherAdjustOrderDetail> getVoucherAdjustOrderDetailListNoPage(
			PCPSWebInput webInput, BigInteger voucherAdjustOrderId) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo())
				|| voucherAdjustOrderId == null)
			return null;

		String sql = "select * from VoucherAdjustOrderDetail where VoucherAdjustOrderId =?1 order by SeqNo";
		Query query = _em
				.createNativeQuery(sql, VoucherAdjustOrderDetail.class);
		query.setParameter(1, voucherAdjustOrderId);
		return query.getResultList();
	}
	/**
	 * 根据凭证序号验证调整单是否，本号段已有待审核的
	 */
	@SuppressWarnings("unchecked")
	public int IsSeqNo(String issueCompanyNo,String startVoucherNo,String endVoucherNo,BigInteger voucherId,String branchNo){
		  String sql = "select * from VoucherAdjustOrderDetail where  VoucherId=?1 "		     
		      +" and VoucherAdjustOrderId  in (select VoucherAdjustOrderId from VoucherAdjustOrder where Status='0' and IssueCompanyNo=?4 and BranchNo=?5)";		

		  Query query = _em.createNativeQuery(sql,VoucherAdjustOrderDetail.class);
		  query.setParameter(1,voucherId);
		  query.setParameter(4,issueCompanyNo);
		  query.setParameter(5,branchNo);
		  List<VoucherAdjustOrderDetail> dalist=query.getResultList();
			if(dalist==null || dalist.size()==0){//空
			    return 0;
			}else{
				VoucherAdjustOrderDetail dil=null;
				for(int i=0;i<dalist.size();i++){
					dil=dalist.get(i);
					if(isVoucherNo(startVoucherNo,endVoucherNo,dil.getStartVoucherNo().trim(),dil.getEndVoucherNo().trim()).equals("error")) 
				        return 10;
				}
				
			}
			return 0;
	}
	/**
    * 验证号段是否重复
    */
	@SuppressWarnings("unchecked")
	private String isVoucherNo(String startvoucherno,String endvoucherno,String dbstartno,String dbendno){
		BigInteger templength = new BigInteger(endvoucherno).subtract(new BigInteger(startvoucherno));
		templength=templength.add(BigInteger.ONE);
		BigInteger verno= new BigInteger(startvoucherno);
		for(int i=0;i<templength.intValue();i++){
			verno=verno.add(new BigInteger(String.valueOf(i)));
			if(verno.intValue()>=Integer.parseInt(dbstartno) && verno.intValue()<=Integer.parseInt(dbendno)){					
				return "error";		 	
			}
		}
		  return "";
	}
}
